//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "JointCalendar.h"
using namespace Cephei::QL::Times::Calendars;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/Period.h>
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Times::Calendars::CJointCalendar::CJointCalendar (Cephei::QL::Times::ICalendar^ c1, Cephei::QL::Times::ICalendar^ c2, Cephei::QL::Times::ICalendar^ c3, Cephei::QL::Times::ICalendar^ c4, Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::JointCalendarRuleEnum>^ r) : CCalendar(CJointCalendar::typeid)
{
    CCalendar^ _Cc1;
    CCalendar^ _Cc2;
    CCalendar^ _Cc3;
    CCalendar^ _Cc4;
    try
    {
#ifdef HANDLE
        _phJointCalendar = NULL;
#endif
        _Cc1 = safe_cast<CCalendar^> (c1);
        _Cc1->Lock();
        QuantLib::Calendar& _c1 = static_cast<QuantLib::Calendar&> (_Cc1->GetReference ()); 
        _Cc2 = safe_cast<CCalendar^> (c2);
        _Cc2->Lock();
        QuantLib::Calendar& _c2 = static_cast<QuantLib::Calendar&> (_Cc2->GetReference ()); 
        _Cc3 = safe_cast<CCalendar^> (c3);
        _Cc3->Lock();
        QuantLib::Calendar& _c3 = static_cast<QuantLib::Calendar&> (_Cc3->GetReference ()); 
        _Cc4 = safe_cast<CCalendar^> (c4);
        _Cc4->Lock();
        QuantLib::Calendar& _c4 = static_cast<QuantLib::Calendar&> (_Cc4->GetReference ()); 
        QuantLib::JointCalendarRule _r = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::JointCalendarRuleEnum>::IsSome::get (r) ? (QuantLib::JointCalendarRule)r->Value : QuantLib::JointCalendarRule::JoinHolidays); //10
        _ppJointCalendar = new boost::shared_ptr<QuantLib::JointCalendar> (new QuantLib::JointCalendar ( _c1,  _c2,  _c3,  _c4,  _r ));
        SetCalendar (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppJointCalendar));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc1 != nullptr) _Cc1->Unlock();
        if (_Cc2 != nullptr) _Cc2->Unlock();
        if (_Cc3 != nullptr) _Cc3->Unlock();
        if (_Cc4 != nullptr) _Cc4->Unlock();
    }
}
Cephei::QL::Times::Calendars::CJointCalendar::CJointCalendar (Cephei::QL::Times::ICalendar^ c1, Cephei::QL::Times::ICalendar^ c2, Cephei::QL::Times::ICalendar^ c3, Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::JointCalendarRuleEnum>^ r) : CCalendar(CJointCalendar::typeid)
{
    CCalendar^ _Cc1;
    CCalendar^ _Cc2;
    CCalendar^ _Cc3;
    try
    {
#ifdef HANDLE
        _phJointCalendar = NULL;
#endif
        _Cc1 = safe_cast<CCalendar^> (c1);
        _Cc1->Lock();
        QuantLib::Calendar& _c1 = static_cast<QuantLib::Calendar&> (_Cc1->GetReference ()); 
        _Cc2 = safe_cast<CCalendar^> (c2);
        _Cc2->Lock();
        QuantLib::Calendar& _c2 = static_cast<QuantLib::Calendar&> (_Cc2->GetReference ()); 
        _Cc3 = safe_cast<CCalendar^> (c3);
        _Cc3->Lock();
        QuantLib::Calendar& _c3 = static_cast<QuantLib::Calendar&> (_Cc3->GetReference ()); 
        QuantLib::JointCalendarRule _r = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::JointCalendarRuleEnum>::IsSome::get (r) ? (QuantLib::JointCalendarRule)r->Value : QuantLib::JointCalendarRule::JoinHolidays); //10
        _ppJointCalendar = new boost::shared_ptr<QuantLib::JointCalendar> (new QuantLib::JointCalendar ( _c1,  _c2,  _c3,  _r ));
        SetCalendar (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppJointCalendar));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc1 != nullptr) _Cc1->Unlock();
        if (_Cc2 != nullptr) _Cc2->Unlock();
        if (_Cc3 != nullptr) _Cc3->Unlock();
    }
}
Cephei::QL::Times::Calendars::CJointCalendar::CJointCalendar (Cephei::QL::Times::ICalendar^ c1, Cephei::QL::Times::ICalendar^ c2, Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::JointCalendarRuleEnum>^ r) : CCalendar(CJointCalendar::typeid)
{
    CCalendar^ _Cc1;
    CCalendar^ _Cc2;
    try
    {
#ifdef HANDLE
        _phJointCalendar = NULL;
#endif
        _Cc1 = safe_cast<CCalendar^> (c1);
        _Cc1->Lock();
        QuantLib::Calendar& _c1 = static_cast<QuantLib::Calendar&> (_Cc1->GetReference ()); 
        _Cc2 = safe_cast<CCalendar^> (c2);
        _Cc2->Lock();
        QuantLib::Calendar& _c2 = static_cast<QuantLib::Calendar&> (_Cc2->GetReference ()); 
        QuantLib::JointCalendarRule _r = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::JointCalendarRuleEnum>::IsSome::get (r) ? (QuantLib::JointCalendarRule)r->Value : QuantLib::JointCalendarRule::JoinHolidays); //10
        _ppJointCalendar = new boost::shared_ptr<QuantLib::JointCalendar> (new QuantLib::JointCalendar ( _c1,  _c2,  _r ));
        SetCalendar (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppJointCalendar));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc1 != nullptr) _Cc1->Unlock();
        if (_Cc2 != nullptr) _Cc2->Unlock();
    }
}
Cephei::QL::Times::Calendars::CJointCalendar::CJointCalendar (boost::shared_ptr<QuantLib::JointCalendar>& childNative, Object^ owner) : CCalendar(CJointCalendar::typeid)
{
#ifdef HANDLE
	_phJointCalendar = NULL;
#endif
	_ppJointCalendar = &childNative;
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppJointCalendar));
}
Cephei::QL::Times::Calendars::CJointCalendar::CJointCalendar (QuantLib::JointCalendar& childNative, Object^ owner) : CCalendar(CJointCalendar::typeid)
{
#ifdef HANDLE
	_phJointCalendar = NULL;
#endif
	_ppJointCalendar = new boost::shared_ptr<QuantLib::JointCalendar> (&childNative);
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppJointCalendar));
    _JointCalendarOwner = owner;
    _CalendarOwner = owner;
}

Cephei::QL::Times::Calendars::CJointCalendar::CJointCalendar (CJointCalendar^ copy) : CCalendar(CJointCalendar::typeid)
{
#ifdef HANDLE
	_phJointCalendar = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppJointCalendar = new boost::shared_ptr<QuantLib::JointCalendar> (copy->GetShared());
        _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppJointCalendar));
    }
}
Cephei::QL::Times::Calendars::CJointCalendar::CJointCalendar (PLATFORM::Type^ t) : CCalendar(CJointCalendar::typeid)
{
#ifdef HANDLE
	_phJointCalendar = NULL;
#endif
	if (!t->IsSubclassOf(CJointCalendar::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::Calendars::CJointCalendar::CJointCalendar (QuantLib::Handle<QuantLib::JointCalendar>& childNative, Object^ owner)  : CCalendar(CJointCalendar::typeid)
{
	_phJointCalendar = &childNative;
	_ppJointCalendar = &static_cast<boost::shared_ptr<QuantLib::JointCalendar>>(childNative.currentLink());
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppJointCalendar));
    _JointCalendarOwner = owner;
}
Cephei::QL::Times::Calendars::CJointCalendar::CJointCalendar (QuantLib::Handle<QuantLib::JointCalendar> childNative)  : CCalendar(CJointCalendar::typeid)
{
	_phJointCalendar = &childNative;
	_ppJointCalendar = &static_cast<boost::shared_ptr<QuantLib::JointCalendar>>(childNative.currentLink());
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppJointCalendar));
}
#endif
#ifdef STRUCT
Cephei::QL::Times::Calendars::CJointCalendar::CJointCalendar (QuantLib::JointCalendar childNative)  : CCalendar(CJointCalendar::typeid)
{
#ifdef HANDLE
	_phJointCalendar = NULL;
#endif
	_ppJointCalendar = new boost::shared_ptr<QuantLib::JointCalendar> (new QuantLib::JointCalendar (childNative));
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppJointCalendar));
}
#endif

Cephei::QL::Times::Calendars::CJointCalendar::~CJointCalendar ()
{
    if (_ppJointCalendar != NULL)
    {
	    delete _ppJointCalendar;
        _ppJointCalendar = NULL;
    }
}
Cephei::QL::Times::Calendars::CJointCalendar::!CJointCalendar ()
{
    if (_ppJointCalendar != NULL)
    {
	    delete _ppJointCalendar;
    }
}
QuantLib::JointCalendar& Cephei::QL::Times::Calendars::CJointCalendar::GetReference ()
{
    if (_ppJointCalendar == NULL) throw REFNEW NativeNullException ();
	return **_ppJointCalendar;
}
boost::shared_ptr<QuantLib::JointCalendar>& Cephei::QL::Times::Calendars::CJointCalendar::GetShared ()
{
    if (_ppJointCalendar == NULL) throw REFNEW NativeNullException ();
	return *_ppJointCalendar;
}
QuantLib::JointCalendar* Cephei::QL::Times::Calendars::CJointCalendar::GetPointer ()
{
    if (_ppJointCalendar == NULL) throw REFNEW NativeNullException ();
	return &**_ppJointCalendar;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::JointCalendar>& Cephei::QL::Times::Calendars::CJointCalendar::GetHandle ()
{
	if (_phJointCalendar == NULL)
	{
		_phJointCalendar = new Handle<QuantLib::JointCalendar> (*_ppJointCalendar);
	}
	return *_phJointCalendar;
}
#endif
bool Cephei::QL::Times::Calendars::CJointCalendar::HasNative () 
{
	return (_ppJointCalendar != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::Calendars::IJointCalendar^ Cephei::QL::Times::Calendars::CJointCalendar_Factory::Create (Cephei::QL::Times::ICalendar^ c1, Cephei::QL::Times::ICalendar^ c2, Cephei::QL::Times::ICalendar^ c3, Cephei::QL::Times::ICalendar^ c4, Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::JointCalendarRuleEnum>^ r)
{
    return REFNEW CJointCalendar ( c1,  c2,  c3,  c4,  r);
}
Cephei::QL::Times::Calendars::IJointCalendar^ Cephei::QL::Times::Calendars::CJointCalendar_Factory::Create (Cephei::QL::Times::ICalendar^ c1, Cephei::QL::Times::ICalendar^ c2, Cephei::QL::Times::ICalendar^ c3, Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::JointCalendarRuleEnum>^ r)
{
    return REFNEW CJointCalendar ( c1,  c2,  c3,  r);
}
Cephei::QL::Times::Calendars::IJointCalendar^ Cephei::QL::Times::Calendars::CJointCalendar_Factory::Create (Cephei::QL::Times::ICalendar^ c1, Cephei::QL::Times::ICalendar^ c2, Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::JointCalendarRuleEnum>^ r)
{
    return REFNEW CJointCalendar ( c1,  c2,  r);
}
